layui.use(['tree','treeSelect','iconPicker','layer','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;
    var treeSelect= layui.treeSelect;//树形下拉框
    var iconPicker = layui.iconPicker;//图标选择

    treeSelect.render({
        elem: '#tree-select',
        data: '/newsType/selectTree',
        type: 'get',
        placeholder: '默认添加顶级菜单',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false
        click: function(data){
            $("#tree-select").val(data.current.id);
        },
        success: function (data) { // 加载完成后的回调函数
            $("#tree-select").val("0");
        }
    });

    //添加图标
    iconPicker.render({
        // 选择器，推荐使用input
        elem: '#iconPicker',
        // 数据类型：fontClass/unicode，推荐使用fontClass
        type: 'fontClass',
        // 是否开启搜索：true/false
        search: true,
        // 是否开启分页
        page: false,
        // 每页显示数量，默认12
        limit: 12,
        // 点击回调
        click: function (data) {
            $("#iconPicker").val('layui-icon ' + data.icon);
        }
    });

    form.on('submit(form-element)', function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        console.log(data.field)
        $.ajax({
            url: '/newsType/add',
            type: 'POST',
            // contentType:"application/json;charset=UTF-8",
            data:data.field,
            async:false,
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("添加失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("无法连接服务器！");
            }
        });
        return false;
    });

});

